import { createStore } from 'vuex';

export default createStore({
    state: {
        userAvatar: null,  // 用户头像
        userInfo: JSON.parse(localStorage.getItem('userInfo')) || {} // 从 localStorage 获取用户信息
    },
    mutations: {
        setUserAvatar(state, avatar) {
            state.userAvatar = avatar;
        },
        setUserInfo(state, userInfo) {
            state.userInfo = userInfo; // 存储用户信息
            localStorage.setItem('userInfo', JSON.stringify(userInfo)); // 将用户信息存储到 localStorage
        }
    },
    actions: {
        updateUserAvatar({ commit }, avatar) {
            commit('setUserAvatar', avatar);
        },
        updateUserInfo({ commit }, userInfo) {
            commit('setUserInfo', userInfo); // 更新用户信息
        }
    },
    getters: {
        userAvatar: state => state.userAvatar,
        userInfo: state => state.userInfo // 获取用户信息
    }
});